'use strict';
const db = uniCloud.database()
const $ = db.command.aggregate
exports.main = async (event, context) => {
	//获取数据库的引用，进行数据库的操作
	const {
		user_id,
		name,
		page = 1,
		pageSize = 10
	} = event;
	let matchObject = {};
	if (name !== '全部') {
		matchObject = {
			classify: name
		}
	};
	const userinfo = await db.collection('user').doc(user_id).get()
	const article_likes_ids = userinfo.data[0].article_likes_ids
	//使用聚合
	let list = await db.collection('article')
		.aggregate()
		.addFields({
			is_like: $.in(['$_id', article_likes_ids])
		})
		.match(matchObject)
		.project({
			content: 0
		})
		//跳过多少数据
		.skip(pageSize * (page - 1))
		//返回数据的条数
		.limit(pageSize)
		.end()

	//获取article表，对表进行操作
	// let list = await db.collection('article')
	// .field({content:false}).get()
	//返回数据给客户端
	return {
		code: 200,
		msg: '数据请求成功',
		data: list.data
	}

	// //返回数据给客户端
	// return event
};
